/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Chang_pass.java
 *
 * Created on Sep 10, 2012, 10:10:55 PM
 */
package Staff;

import ConnnectData.DataConnect;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author HP
 */
public class Chang_pass extends javax.swing.JFrame {

    /** Creates new form Chang_pass */
    Connection Con;
    Statement Sta;
    ResultSet Res;
    PreparedStatement pst;
    String UserName;
    public Chang_pass() {
        initComponents();
        lblID.setText("");
        lblNewPass.setText("");
        lblCurrentPAss.setText("");
        lblConfimNewPass.setText("");
        lblMes.setText("");
        
        
        try {
            Con = DataConnect.connectDB();
            Sta = Con.createStatement();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtId = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtCurrentPass = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        txtNewPass = new javax.swing.JPasswordField();
        jLabel4 = new javax.swing.JLabel();
        txtConfimNewPass = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();
        lblID = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        lblCurrentPAss = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        lblNewPass = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        lblConfimNewPass = new javax.swing.JLabel();
        lblMes = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        mnUpdate = new javax.swing.JMenu();
        mnClose = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Your Id");

        jLabel2.setText("Current password ");

        jLabel3.setText("New PassWord");

        jLabel4.setText("Confim New PassWord");

        jLabel5.setText("*");

        lblID.setText("Message");

        jLabel7.setText("*");

        lblCurrentPAss.setText("Message");

        jLabel9.setText("*");

        lblNewPass.setText("Message");

        jLabel11.setText("*");

        lblConfimNewPass.setText("Message");

        lblMes.setText("Message");

        mnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Untitled.jpg"))); // NOI18N
        mnUpdate.setText("Update");
        mnUpdate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnUpdateMouseClicked(evt);
            }
        });
        mnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnUpdateActionPerformed(evt);
            }
        });
        jMenuBar1.add(mnUpdate);

        mnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/exit.png"))); // NOI18N
        mnClose.setText("Close");
        mnClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnCloseMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnClose);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtConfimNewPass)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lblID))
                            .addComponent(txtCurrentPass, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
                            .addComponent(txtNewPass))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblCurrentPAss))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblNewPass))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblConfimNewPass))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(186, 186, 186)
                        .addComponent(lblMes)))
                .addContainerGap(93, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(lblID))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtCurrentPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel7)
                        .addComponent(lblCurrentPAss)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNewPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9)
                            .addComponent(lblNewPass))))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtConfimNewPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel11)
                            .addComponent(lblConfimNewPass))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addComponent(lblMes)
                .addGap(29, 29, 29))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void mnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnUpdateActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_mnUpdateActionPerformed

private void mnUpdateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnUpdateMouseClicked
// TODO add your handling code here:
    if(txtId.getText().equals(""))
        {
            lblID.setText("You need input your id !");
            return;
        }
        if(txtCurrentPass.getText().equals(""))
        {
            lblCurrentPAss.setText("Current pass not null !");
            return;
        }
        if(txtCurrentPass.getText().length()>15)
        {
            lblCurrentPAss.setText("Current pass not validate !");
            return;
        }
        if(txtNewPass.getText().equals(""))
        {
            lblNewPass.setText("new pass not null !");
            return;
        }
        if(txtNewPass.getText().length()>15)
        {
            lblNewPass.setText("new pass must < 15 character !");
            return;
        }
        if(txtConfimNewPass.getText().equals(""))
        {
            lblConfimNewPass.setText("Cofilm pass not null !");
            return;
        }
        if(!txtConfimNewPass.getText().equals(txtNewPass.getText().trim()))
        {
            lblConfimNewPass.setText("Password not the same !");
            return;
        }
        try {
            Res = Sta.executeQuery("Select *from Staff where Staff_Id='"+txtId.getText()+"' and Staff_Password='"+txtCurrentPass.getText()+"'");
            if(Res.next())
            {
                pst = Con.prepareStatement("Update Staff set Staff_Password=? where Staff_Id=?");
                pst.setString(1, txtNewPass.getText().trim());
                pst.setString(2, txtId.getText().trim());
                pst.executeUpdate();
                this.dispose();
                JOptionPane.showMessageDialog(null, "Change successful password ");
            }else
            {
                JOptionPane.showMessageDialog(null, "Infomation not validate !");
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
}//GEN-LAST:event_mnUpdateMouseClicked

private void mnCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnCloseMouseClicked
// TODO add your handling code here:
     int Close=JOptionPane.showConfirmDialog(null, "Are you sure want close  !");
       if(Close==0)
       this.dispose();
}//GEN-LAST:event_mnCloseMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Chang_pass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Chang_pass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Chang_pass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Chang_pass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Chang_pass().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JLabel lblConfimNewPass;
    private javax.swing.JLabel lblCurrentPAss;
    private javax.swing.JLabel lblID;
    private javax.swing.JLabel lblMes;
    private javax.swing.JLabel lblNewPass;
    private javax.swing.JMenu mnClose;
    private javax.swing.JMenu mnUpdate;
    private javax.swing.JPasswordField txtConfimNewPass;
    private javax.swing.JPasswordField txtCurrentPass;
    private javax.swing.JTextField txtId;
    private javax.swing.JPasswordField txtNewPass;
    // End of variables declaration//GEN-END:variables
}
